import axios from 'axios'
import router from '@router'
import Vue from 'vue'
const vm = new Vue()
const request = axios.create({
    baseURL: '/api',
    timeout: 5000
})
const obtain = axios.create({
    baseURL: '/conner',
    timeout: 5000,
    headers: {
        'content-type': 'application/x-www-form-urlencoded'
    }
})
request.interceptors.request.use(function(config) {
    // Do something before request is sent
    if (localStorage.getItem('token')) {
        config.headers.Authorization = localStorage.getItem('token')
    }
    vm.$showLoding()
    console.log(vm, "拦截器");
    return config;
}, function(error) {
    // Do something with request error
    return Promise.reject(error);
});

// Add a response interceptor
request.interceptors.response.use(function(res) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    // console.log(router, "拦截器");
    vm.$hideLoading()
    return res;
}, function(error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
});




obtain.interceptors.request.use(function(config) {
    // Do something before request is sent
    if (localStorage.getItem('token')) {
        config.headers.Authorization = localStorage.getItem('token')
    }
    
    vm.$showLoding()
    console.log(vm, "拦截器");
    console.log("qingqiu");
    return config;
}, function(error) {
    // Do something with request error
    return Promise.reject(error);
});

// Add a response interceptor
obtain.interceptors.response.use(function(response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    console.log("fanhuile");
    vm.$hideLoading()
    if(response.data.code===200){
        vm.$router.push('/login')
    }
    return response;
}, function(error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    console.log("fanhuile");
    return Promise.reject(error);
});


export {
    request,
    obtain
}